function HylPage(page) {
  this.instance;
  this.table = null;
  this.pagination = null;// 分页组件 待绑定
  this.data = null;// 所有下载记录=当前本地的所有数据 ,每次下载重新刷新
  this.rcStart = 0; // 服务端记录的第几条
  this.rcNum = 0; // 从服务器取出多少条
  this.rcNPageend = 0;// 最后一页的记录数
  this.pgMax = 5; // 最多显示页数
  this.pgNum = 0; // 数据可以分成多少页
  this.pgSize = 10; // 每页默认10条
  this.pgIndex = 1;// 当前是第几页
  this.pgPrev = 1; // 上一组的起始页
  this.pgStart = 1; // 当前窗口的起始页
  this.pgEnd = 1; // 下载记录的结束页 = 起始页+ 页数
  this.goPage = null;// table对象加载数据的回调函数
  var ta = this;
  this.getMaxRcNum = function() {
    return ta.pgMax * ta.pgSize;
  } // 分页窗口最多显示多少条
  this.getStart = function() {
    return (ta.pgIndex - ta.pgStart) * ta.pgSize;
  }
  // 绑定分页组件tableobj,pageobj,rs,pindex,psize
  this.bind = function(obj) {
    if (obj.tableobj)
      ta.table = obj.tableobj;
    if (obj.pageobj)
      ta.pagination = obj.pageobj;
    if (obj.pindex && obj.pindex > 0) {
      ta.rcStart = parseInt(obj.pindex) * ta.pgSize;
      ta.pgIndex = ta.pgStart = parseInt(obj.pindex);
    }
    if (obj.psize && obj.psize > 0)
      ta.pgSize = parseInt(obj.psize);
    if (obj.rs) {
      ta.data = obj.rs;
      // 根据分页属性绘制分页组件// 跳转到指定页面
      ta.ini().page(ta.pgIndex);
    }
  }
  this.setpgSize = function(psize) {
    if (psize)
      ta.pgSize = psize;
  }
  // 绘制
  this.ini = function() {
    if (!ta.pagination || !ta.table)// 如果没有绑定分页组件和datatables对象则退出
      return ta;
    // 设置本地记录数
    ta.rcNum = ta.data.length;
    if (ta.rcNum == 0)
      return ta;
    if (ta.getMaxRcNum() < ta.rcNum)
      ta.rcNum = ta.getMaxRcNum();
    // 设计记录可以分为多少页,当前窗口结束页
    ta.pgNum = Math.ceil(ta.rcNum / ta.pgSize);
    ta.pgEnd = ta.pgStart + ta.pgNum - 1;
    // 设置最后一页的条数
    ta.rcNPageend = ta.rcNum % ta.pgSize;
    if (ta.rcNPageend == 0)
      ta.rcNPageend = ta.pgSize;
    // 设置当前窗口的前一组起始页
    ta.pgPrev = 1;
    if (ta.pgStart >= ta.pgMax) {
      ta.pgPrev = ta.pgStart - ta.pgMax + 1;
    }
    return ta;
  }
  // 显示第几页
  this.page = function(index) {
    if (ta.pgStart > index || (ta.pgStart + ta.pgNum) < index)
      return;
    ta.pgIndex = index;
    // 清空,输出分页按钮
    ta.pagination.html("");
    if (ta.pgStart > 1)
      ta.pagination.append($('<li class="btn-gpage" page="' + ta.pgPrev
          + '"><a><i class="ace-icon fa fa-angle-double-left"></i></a></li>'))
    for (var i = 0; i < ta.pgNum; i++) {
      var a = ta.pgStart + i;
      var clas = (a == ta.pgIndex) ? 'active' : '';
      ta.pagination.append($('<li class="btn-page ' + clas + '" page="' + a + '"><a >' + a
          + "</a></li>"))
    }
    if (ta.pgNum == ta.pgMax)
      ta.pagination.append($('<li class="btn-gpage" page="' + ta.pgEnd
          + '"><a><i class="ace-icon fa fa-angle-double-right"></i></a></li>'))
    if (ta.goPage) {
      var s = ta.getStart();
      var e = s;
      if (ta.pgIndex == ta.pgEnd)
        e += ta.rcNPageend;
      else
        e += ta.pgSize;
      ta.goPage(s, e);
    }
  }
  this.goPage = function(m, n) {
    ta.table.rows().remove().draw();
    var data = ta.data.slice(m, n);
    ta.table.rows.add(data).draw();
  };
}
HylPage.getInstance = function() {
  if (!this.instance) {
    this.instance = new HylPage();
  }
  return this.instance;
};
var PG = HylPage.getInstance();
$(document).on('click', '.btn-page', function(){
  var i = $(this).attr("page");
  PG.page(i);
});
$(document).on('click', '.btn-gpage', function(){
  var i = $(this).attr("page");
  PG.load(i);
});